package com.tsingsoft.collect.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tsingsoft.collect.dao.WeatherDao;
import com.tsingsoft.common.dao.DBAccess;

public class WeatherDaoImpl implements WeatherDao {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	public String getPointIdByWindfarmId(String forecastnumber,String windfarmId){
		String pointId = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		DBAccess dao = null;
		
		String sqlsta = "SELECT POINTID FROM BAS_WEATHER_POINT WHERE WINDFARM_IDS=? and forecastnumber=? " ;
		try{
			dao = new DBAccess();
			conn = dao.getConnection();
			pstmt = conn.prepareStatement(sqlsta);
			pstmt.setString(1, windfarmId);
			pstmt.setString(2, forecastnumber);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				pointId = rs.getString("POINTID");
				break;
			}
		}catch(Exception e){
			logger.error(ExceptionUtils.getStackTrace(e));
		}finally{
			dao.close(rs, pstmt, conn);
		}
		
		return pointId;
	}

	public String getPointIdByWindfarmId(String forecastnumber,String windfarmId,String name){
		String pointId = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		DBAccess dao = null;
		
		String sqlsta = "SELECT POINTID FROM BAS_WEATHER_POINT WHERE WINDFARM_IDS=? and forecastnumber=? and pointid like ?" ;
		try{
			dao = new DBAccess();
			conn = dao.getConnection();
			pstmt = conn.prepareStatement(sqlsta);
			pstmt.setString(1, windfarmId);
			pstmt.setString(2, forecastnumber);
			pstmt.setString(3, name+"%");
			rs = pstmt.executeQuery();
			while (rs.next()) {
				pointId = rs.getString("POINTID");
				break;
			}
		}catch(Exception e){
			logger.error(ExceptionUtils.getStackTrace(e));
		}finally{
			dao.close(rs, pstmt, conn);
		}
		
		return pointId;
	}
	
	public String getPointIdByWfIdAndStatus(String forecastnumber,String windfarmId, String status){
		String pointId = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		DBAccess dao = null;
		
		String sqlsta = "SELECT POINTID FROM BAS_WEATHER_POINT WHERE WINDFARM_IDS=? and forecastnumber=? and POINTID like ?" ;
		try{
			dao = new DBAccess();
			conn = dao.getConnection();
			pstmt = conn.prepareStatement(sqlsta);
			pstmt.setString(1, windfarmId);
			pstmt.setString(2, forecastnumber);
			pstmt.setString(3, "%" + status + "%");
			rs = pstmt.executeQuery();
			while (rs.next()) {
				pointId = rs.getString("POINTID");
				break;
			}
		}catch(Exception e){
			logger.error(ExceptionUtils.getStackTrace(e));
		}finally{
			dao.close(rs, pstmt, conn);
		}
		
		return pointId;
	}
}
